package cn.eeepay.framework.dao.wetchatPublic;

import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.wetchatPublic.PublicAccountMsgTempEntity;
import cn.eeepay.framework.util.StringUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

/**
 * 公众号模板消息表
 * 
 * @author quanhz
 * @email 
 * @date 2020-07-29 15:31:47
 */

public interface PublicAccountMsgTempDao  {

    @SelectProvider(type = PublicAccountMsgTempDao.SqlProvider.class, method = "queryPage")
    @ResultType(List.class)
    List<PublicAccountMsgTempEntity> queryPage(@Param("page") Page<PublicAccountMsgTempEntity> page, @Param("info") PublicAccountMsgTempEntity info);

    @Select("select pub_name,pub_acc from public_account_msg_temp group by pub_name,pub_acc order by create_time asc")
    List<PublicAccountMsgTempEntity> findAllpubNames();

    @Select("select id,title from public_account_msg_temp group by title order by create_time asc")
    List<PublicAccountMsgTempEntity> findAllTitles();

    @Select("select * from public_account_msg_temp where id = #{id}")
    PublicAccountMsgTempEntity getById(Integer id);

    @Select("select openid from mer_wechat_info where public_account=#{pubAcc}")
    String getOpenIdByPubAcc(String pubAcc);


    class SqlProvider {
        public String queryPage(Map<String, Object> param) {
            final PublicAccountMsgTempEntity info = (PublicAccountMsgTempEntity) param.get("info");
            return new SQL() {{
                SELECT("*");
                FROM("public_account_msg_temp");
                if(!StringUtil.isBlank(info.getPubAcc())){
                    WHERE(" pub_acc=#{info.pubAcc}");
                }

                if(!StringUtil.isBlank(info.getTitle())){
                    WHERE("title = #{info.title}");
                }
            }}.toString();
        }
    }
}
